package com.tech.arno.spicy.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tech.arno.spicy.R;
import com.tech.arno.spicy.module.recommand.RecommandValue;
import com.tech.arno.spicy.share.ShareDialog;
import com.tech.arno.spicy.ui.activity.MyPhotoViewActivity;
import com.tech.arno.spicy.util.ImageLoaderManager;
import com.tech.arno.spicy.util.SpicyUtil;
import com.tech.arno.spicysdk.videosdk.core.AdContextInterface;
import com.tech.arno.spicysdk.videosdk.core.context.VideoContext;
import com.util.LogUtil;
import com.util.Utils;

import java.util.ArrayList;

import cn.sharesdk.framework.Platform;
import de.hdodenhof.circleimageview.CircleImageView;

/*
*   @package:     com.tech.arno.spicy.ui.adapter
*   @fileName:    CourseAdapter
*   @author:    ArnoFrost
*   @date:  2017/12/18 0018 10:14
*   @desc:     首页加载综合Adapter
*/

public class CourseAdapter extends BaseAdapter {
    private static final String TAG = "CourseAdapter";
    //Listview不同类型item标识
    private static final int CARD_COUNT = 4;
    //视频播放布局
    private static final int VIDOE_TYPE = 0x00;
    //多图动态加载布局
    private static final int CARD_TYPE_MULTY = 0x01;
    //单图加载布局
    private static final int CARD_TYPE_SIMPLE = 0x02;
    //
    private static final int CARD_VIEW_PAGER = 0x03;

    private Context mContext;
    private LayoutInflater mInflate;
    private ViewHolder mViewHolder;
    //    private AdContext mAdsdkContext;
    private VideoContext mAdsdkContext;

    private ArrayList<RecommandValue> mData;

    private ImageLoaderManager mImageLoader;

    public CourseAdapter(Activity mContext, ArrayList<RecommandValue> data) {
        this.mContext = mContext;
        mData = data;
        mInflate = LayoutInflater.from(mContext);
        mImageLoader = ImageLoaderManager.getInstance(mContext);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return CARD_COUNT;
    }

    /**
     * @param position 位置
     * @return
     * @function 通知Adpater使用哪中类型的tiem去加载
     * @author ArnoFrost
     * @time 2017/12/9 0009  12:50
     */
    @Override
    public int getItemViewType(int position) {
        RecommandValue value = (RecommandValue) getItem(position);
        return value.type;
    }


    /**
     * @return view
     * @function
     * @author ArnoFrost
     * @time 2017/12/9 0009  12:50
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //1. 获取数据的type类型
        int type = getItemViewType(position);
        final RecommandValue value = (RecommandValue) getItem(position);
        if (convertView == null) {//初始化ViewHolder中所用到的控件
            switch (type) {
                case VIDOE_TYPE:
                    //显示video卡片
                    mViewHolder = new ViewHolder();
                    convertView = mInflate.inflate(R.layout.item_video_layout, parent, false);
                    mViewHolder.mVieoContentLayout = (RelativeLayout)
                            convertView.findViewById(R.id.video_ad_layout);
                    mViewHolder.mShareView = (ImageView) convertView.findViewById(R.id.item_share_view);
                    Log.e("adapter", value.resource + " title:" + value.title);
                    mViewHolder.mLogoView = (CircleImageView) convertView
                            .findViewById(R.id.item_logo_view);
                    mViewHolder.mTitleView = (TextView) convertView
                            .findViewById(R.id.item_title_view);
                    mViewHolder.mInfoView = (TextView) convertView
                            .findViewById(R.id.item_info_view);
                    mViewHolder.mFooterView = (TextView) convertView
                            .findViewById(R.id.item_footer_view);
                    //为对应布局创建播放器
                    mAdsdkContext = new VideoContext(mViewHolder.mVieoContentLayout,
                            new Gson().toJson(value), null);
                    mAdsdkContext.setAdResultListener(new AdContextInterface() {
                        @Override
                        public void onAdSuccess() {
                        }

                        @Override
                        public void onAdFailed() {
                            Toast.makeText(mContext, "加载视频失败..", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onClickVideo(String url) {
                            LogUtil.d(TAG, "onClickVideo: Small");
                            //点击视频
//                            Intent intent = new Intent(mContext, AdBrowserActivity.class);
//                            intent.putExtra(AdBrowserActivity.KEY_URL, url);
//                            mContext.startActivity(intent);

                        }
                    });

                    break;
                case CARD_TYPE_MULTY:
                    mViewHolder = new ViewHolder();
                    convertView = mInflate.inflate(R.layout.item_product_card_multy_layout, parent, false);
                    mViewHolder.mLogoView = (CircleImageView) convertView
                            .findViewById(R.id.item_logo_view);
                    mViewHolder.mTitleView = (TextView) convertView
                            .findViewById(R.id.item_title_view);
                    mViewHolder.mInfoView = (TextView) convertView
                            .findViewById(R.id.item_info_view);
                    mViewHolder.mFooterView = (TextView) convertView
                            .findViewById(R.id.item_footer_view);
                    mViewHolder.mPriceView = (TextView) convertView
                            .findViewById(R.id.item_price_view);
                    mViewHolder.mFromView = (TextView) convertView
                            .findViewById(R.id.item_from_view);
                    mViewHolder.mZanView = (TextView) convertView
                            .findViewById(R.id.item_zan_view);
                    //多图加载水平布局
                    mViewHolder.mProductLayout = convertView
                            .findViewById(R.id.product_photo_layout);

                    break;
                case CARD_TYPE_SIMPLE:
                    mViewHolder = new ViewHolder();
                    convertView = mInflate.inflate(R.layout.item_product_card_simple_layout, parent, false);

                    mViewHolder.mLogoView = (CircleImageView) convertView
                            .findViewById(R.id.item_logo_view);
                    mViewHolder.mTitleView = (TextView) convertView
                            .findViewById(R.id.item_title_view);
                    mViewHolder.mInfoView = (TextView) convertView
                            .findViewById(R.id.item_info_view);
                    mViewHolder.mFooterView = (TextView) convertView
                            .findViewById(R.id.item_footer_view);
                    mViewHolder.mProductView = (ImageView) convertView
                            .findViewById(R.id.product_photo_view);
                    mViewHolder.mPriceView = (TextView) convertView
                            .findViewById(R.id.item_price_view);
                    mViewHolder.mFromView = (TextView) convertView
                            .findViewById(R.id.item_from_view);
                    mViewHolder.mZanView = (TextView) convertView
                            .findViewById(R.id.item_zan_view);
                    break;
                case CARD_VIEW_PAGER:
                    mViewHolder = new ViewHolder();
                    convertView = mInflate.inflate(
                            R.layout.item_product_card_view_pager_layout, parent, false);

                    mViewHolder.mViewPager = (ViewPager) convertView.findViewById(R.id.pager);
                    //设置间隔
                    mViewHolder.mViewPager.setPageMargin(
                            Utils.dip2px(mContext, 12)
                    );
                    //为ViewPager填充数据

                    ArrayList<RecommandValue> recommandValues
                            = SpicyUtil.handleData(value);

                    mViewHolder.mViewPager.setAdapter(
                            new HotSalePagerAdapter(mContext, recommandValues)
                    );
                    //使得一开始就处于中间的位置,所以左右两边无限循环滑动
                    mViewHolder.mViewPager.setCurrentItem(recommandValues.size() * 100);
                    break;
            }
            assert convertView != null;
            convertView.setTag(mViewHolder);
        } else {//有可用的ConvertView

            mViewHolder = (ViewHolder) convertView.getTag();
        }
        //开始绑定
        //填充item的数据
        if (type != CARD_VIEW_PAGER) {
            mImageLoader.displayImage(mViewHolder.mLogoView, value.logo);
            mViewHolder.mTitleView.setText(value.title);
            mViewHolder.mInfoView.setText(value.info.concat(mContext.getString(R.string.tian_qian)));
            mViewHolder.mFooterView.setText(value.text);
        }

        switch (type) {
            case VIDOE_TYPE:
                mViewHolder.mShareView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ShareDialog dialog = new ShareDialog(mContext, false);
                        dialog.setShareType(Platform.SHARE_VIDEO);
                        dialog.setShareTitle(value.title);
                        dialog.setShareTitleUrl(value.site);
                        dialog.setShareText(value.text);
                        dialog.setShareSite(value.title);
                        dialog.setShareTitle(value.site);
                        dialog.setUrl(value.resource);
                        dialog.show();
                    }
                });
                break;
            case CARD_TYPE_MULTY:
                mViewHolder.mPriceView.setText(value.price);
                mViewHolder.mFromView.setText(value.from);
                mViewHolder.mZanView.setText(mContext.getString(R.string.dian_zan).concat(value.zan));
                //为类型1的三个imageview加载远程图片。
//                mImageLoader.displayImage(mViewHolder.mProductOneView, value.url.get(0));
//                mImageLoader.displayImage(mViewHolder.mProductTwoView, value.url.get(1));
//                mImageLoader.displayImage(mViewHolder.mProductThreeView, value.url.get(2));
                //动态添加ImageView
                mViewHolder.mProductLayout.removeAllViews();//删除已有的图片
                ImageView imageView;
                for (String url : value.url) {
                    imageView = createImageView(url);
                    mViewHolder.mProductLayout.addView(imageView);
                    //为动态加载布局添加点击事件
                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, MyPhotoViewActivity.class);
                            intent.putStringArrayListExtra(MyPhotoViewActivity.PHOTO_LIST, value.url);
                            mContext.startActivity(intent);
                        }
                    });

                }
                break;
            case CARD_TYPE_SIMPLE:
                mViewHolder.mPriceView.setText(value.price);
                mViewHolder.mFromView.setText(value.from);
                mViewHolder.mZanView.setText(mContext.getString(R.string.dian_zan).concat(value.zan));
                //为单个ImageView加载远程图片
                mImageLoader.displayImage(mViewHolder.mProductView, value.url.get(0));
                break;

            default:
                break;
        }
        return convertView;
    }

    private ImageView createImageView(String url) {
        ImageView imageView = new ImageView(mContext);
        //与要与添加到的ViewGroup保持一致
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                Utils.dip2px(mContext, 100), LinearLayout.LayoutParams.MATCH_PARENT);
        params.leftMargin = Utils.dip2px(mContext, 5);
        imageView.setLayoutParams(params);
        mImageLoader.displayImage(imageView, url);

        return imageView;
    }

    //自动播放方法
    public void updateAdInScrollView() {
        if (mAdsdkContext != null) {
            mAdsdkContext.updateAdInScrollView();
        }
    }

    /**
     * @author ArnoFrost
     * @function 用来缓存创建好的Item
     * @time 2017/12/9 0009  12:52
     */

    private static class ViewHolder {
        //所有Card共有属性
        private CircleImageView mLogoView;
        private TextView mTitleView;
        private TextView mInfoView;
        private TextView mFooterView;
        //Video Card特有属性
        private RelativeLayout mVieoContentLayout;
        private ImageView mShareView;

        //Video Card外所有Card具有属性
        private TextView mPriceView;
        private TextView mFromView;
        private TextView mZanView;
        //Card Multy特有属性
        private LinearLayout mProductLayout;
        private ImageView mProductOneView;
        private ImageView mProductTwoView;
        private ImageView mProductThreeView;
        //Card Simple特有属性
        private ImageView mProductView;
        //Card Pager特有属性
        private ViewPager mViewPager;
    }

    public interface multyImageClick {
        void ImageClick();
    }


}
